"""
演示JSON数据和python字典的相互转换
"""
import json
# 准备列表，列表内每一个元素都是字典，将其转换为JSON
data = [{"name": "zhang", "age": 11}, {"name": "王大谁", "age": 13},{"name": "张小虎", "age": 15}]
json_str = json.dumps(data, ensure_ascii=False)
print(json_str)
# 准备字典，将字典转换为json
d = {"name":"周杰伦", "addr": "台北"}
json_str = json.dumps(d,ensure_ascii=False)
print(json_str)
# 将json字符串转换为python数据类型[{k:v,k:v}],{k:v,k:v}]
s = '[{"name": "zhang", "age": 11}, {"name": "王大谁", "age": 13}, {"name": "张小虎", "age": 15}]'
l = json.loads(s)
print(type(l))
print(l)
# 将json字符串转换为python数据类型{k:v,k:v}
s = '{"name": "周杰伦", "addr": "台北"}'
d = json.loads(s)
print(type(d))
print(d)